package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineRequestItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class QuarantineRequestThread extends BasicThread {

	public interface QuarantineRequestThreadCallback {
		public void OnQuarantineRequestCompleted(boolean isNetworkConntected, boolean results, String mResult);
		public void OnQuarantineRequestError(ErrorItem errorItem);
	}

	private String url;
	private QuarantineRequestThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private boolean isFirst, pageState;
	private String offCd;

	//처음 페이지를 호출할때
	public QuarantineRequestThread(Context context, QuarantineRequestThreadCallback callback, String offCd, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.isFirst = true;
	}
	
	public QuarantineRequestThread(Context context,
			QuarantineRequestThreadCallback callback, HashMap<String, String> searchMap, boolean pageState, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
		this.isFirst = false;
		this.pageState = pageState;
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String sDate = BrUtilManager.getCurrentDateTime(0);
		String eDate = BrUtilManager.getCurrentDateTime(0);
		String param = "";
		if(isFirst){			//최초요청
			map.put(Const.SEARCHOFFCD, offCd);
			map.put(Const.SEARCHSDATE, sDate);
			map.put(Const.SEARCHEDATE, eDate);
			param = parameterReturn(Const.REQ002, (HashMap<String, String>) map);
			Log.i("result", "result -->"+param);
		}else{					//조건요청
			param = parameterReturn(Const.REQ003, searchMap);
			Log.i("result", "result -->"+param);
		}
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		try {
			json = new JSONObject(returnJson);
			if(json == null) {
				callback.OnQuarantineRequestError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			ArrayList<QuarantineRequestItem> arrQuarantineItem = null;
			if(pageState){
				arrQuarantineItem = appStatus.getArrQuarantineItem();
			}else{
				arrQuarantineItem = new ArrayList<QuarantineRequestItem>();
			}
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				appStatus.setArrQuarantineItem(arrQuarantineItem);
				callback.OnQuarantineRequestError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				QuarantineRequestItem quarantineItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject quarantine = arrayLogin.getJSONObject(i);
					
					quarantineItem = new QuarantineRequestItem();
					quarantineItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.OFFCD)));
					quarantineItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.RECNO)));
					quarantineItem.setTraDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.TRADE)));
					quarantineItem.setRceptDate(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.RCEPTDATE)));
					quarantineItem.setQrantHde(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.QRANTHDE)));
					quarantineItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.VENNM)));
					quarantineItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.NATNM)));
					quarantineItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.PRDLSTNM)));
					quarantineItem.setInsPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.INSPLACE)));
					quarantineItem.setTotalRows(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.TOTALROWS)));
					quarantineItem.setTrnCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestItem.TRNCD)));
					
					arrQuarantineItem.add(quarantineItem);
				}
				appStatus.setArrQuarantineItem(arrQuarantineItem);
				callback.OnQuarantineRequestCompleted(true, true, mResult);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
